package com.malinkang.sqlite;

import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.BaseColumns;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by malinkang on 16/3/28.
 */
public class InsertOrUpdateActivity extends AppCompatActivity {
    @Bind(R.id.iv_avatar) CircleImageView mAvatarImageView;
    @Bind(R.id.et_username) EditText mUsernameEditText;
    @Bind(R.id.et_phone) EditText mPhoneEditText;
    @Bind(R.id.btn_add) Button mAddBtn;
    private Uri mImageUri;
    //类型
    public User mUser;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_insert_or_update);
        ButterKnife.bind(this);
        mUser = getIntent().getParcelableExtra("user");
        if (mUser != null) {
            mImageUri = Uri.parse(mUser.getAvatar());
            mAvatarImageView.setImageURI(mImageUri);
            mUsernameEditText.setText(mUser.getUsername());
            mPhoneEditText.setText(mUser.getPhone());
        }
    }
    @OnClick(R.id.btn_add)
    public void insertOrUpdate() {
        String name = mUsernameEditText.getText().toString();
        String phone = mPhoneEditText.getText().toString();
        ContentValues values = new ContentValues();
        if (mImageUri == null) {
            Toast.makeText(InsertOrUpdateActivity.this, "请上传头像", Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(name)) {
            Toast.makeText(InsertOrUpdateActivity.this, "名字不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(phone)) {
            Toast.makeText(InsertOrUpdateActivity.this, "手机号不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        values.put(DBOpenHelper.COLUMN_AVATAR, mImageUri.toString());
        values.put(DBOpenHelper.COLUMN_USERNAME, name);
        values.put(DBOpenHelper.COLUMN_PHONE, phone);
        //往User.db 插入数据
        if (mUser == null) {
            getContentResolver().insert(Uri.parse("content://" + UserContentProvider.sAuthority + "/" + DBOpenHelper.USER_TABLE_NAME), values);
        } else {
            getContentResolver()
                    .update(Uri.parse("content://" + UserContentProvider.sAuthority + "/" + DBOpenHelper.USER_TABLE_NAME),
                            values, BaseColumns._ID + "=?", new String[]{mUser.getId() + ""});
        }
        //往User2.db 插入

        if (mUser == null) {
            //插入
            mUser = new User();
            mUser.setAvatar(mImageUri.toString());
            mUser.setUsername(name);
            mUser.setPhone(phone);
            mUser.save();
        } else {
            //update
        }
        finish();
    }

    @OnClick(R.id.iv_avatar)
    public void addAvatar() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setItems(
                new String[]{"拍照", "从图库选择"},
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        switch (which) {
                            case 0:
                                mImageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new ContentValues());
                                if (mImageUri != null) {
                                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                                    intent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri);
                                    startActivityForResult(intent, 0);
                                }
                                break;
                            case 1:
                                Intent intent = new Intent("android.intent.action.PICK");
                                intent.setType("image/*");
                                startActivityForResult(intent, 1);
                                break;
                        }

                    }
                }
        ).show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case 0:
                    break;
                case 1:
                    mImageUri = intent.getData();
                    break;
            }
            mAvatarImageView.setImageURI(mImageUri);

        }
    }


}
